<?php
/**
 * (新)备货记录/日志
 * (model)
 */
namespace App\Models\ProductStockup;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class StockupApplyLog extends Model{

    protected $table = 'product_stockup_apply_log';

    protected $guarded = [];

    const LOG_STATUS_APPLY_NO  = 0; //未申请(初始状态)
    const LOG_STATUS_APPLY_YES = 1; //申请中（已申请）
    const LOG_STATUS_CANCEL    = 2; //撤回申请
    const LOG_STATUS_REJECT    = 3; //审核驳回
    const LOG_STATUS_APPROVE   = 4; //审核通过
    const LOG_STATUS_CONFIRM   = 5; //已确认
    /**
     * log类型
     * @var string[]
     */
    public static $LOG_STATUS = [
        self::LOG_STATUS_APPLY_NO   => '未申请',
        self::LOG_STATUS_APPLY_YES  => '申请中',
        self::LOG_STATUS_CANCEL     => '撤回申请',
        self::LOG_STATUS_REJECT     => '审核驳回',
        self::LOG_STATUS_APPROVE    => '审核通过',
        self::LOG_STATUS_CONFIRM    => '已确认',
    ];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 备货日志（新）
     * @param $stockup
     */
    public static function _addStockupLog($stockup){
        $log = [];
        $log['area_psku'] = $stockup->area_psku;
        $log['classify_id'] = $stockup->classify_id;
        $log['area_id'] = $stockup->area_id;
        $log['psku_id'] = $stockup->psku_id;
        $log['ratio'] = $stockup->ratio->ratio ?? 0;
        $log['fba_instant_num'] = $stockup->fba_instant_num;
        $log['fba_transit_num'] = $stockup->fba_transit_num;
        $log['cn_instant_num'] = $stockup->cn_instant_num;
        $log['cn_transit_num'] = $stockup->cn_transit_num;
        $log['os_instant_num'] = $stockup->os_instant_num;
        $log['os_transit_num'] = $stockup->os_transit_num;

        $log['apply_num'] = $stockup->apply_num;
        $log['apply_remark'] = $stockup->apply_remark;
        $log['apply_time'] = $stockup->apply_time ?? date("Y-m-d H:i:s", time());
        $log['apply_admin_id'] = $stockup->apply_admin_id;
        $log['approval_num'] = $stockup->approval_num;
        $log['approval_remark'] = $stockup->approval_remark;
        $log['approval_time'] = $stockup->approval_time ?? date("Y-m-d H:i:s", time());
        $log['approval_admin_id'] = $stockup->approval_admin_id;
        $log['confirm_num'] = $stockup->confirm_num;
        $log['confirm_remark'] = $stockup->confirm_remark;
        $log['confirm_time'] = $stockup->confirm_time ?? date("Y-m-d H:i:s", time());
        $log['confirm_admin_id'] = $stockup->confirm_admin_id;
        $log['log_status'] = $stockup->status;
        $log['log_message'] = '【备货：'.Stockup::$APPLY_STATUS[$stockup->status].'】';
        StockupApplyLog::query()->create($log);
    }

}
